package com.hbnu.jdbc;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author kittychen
 * @since 2025-10-22 17:40
 */
public class JdbcDemo03 {

    public static void main(String[] args) {

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            // 通过反射读取数据库配置文件
            InputStream resourceAsStream = JdbcDemo03.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties properties = new Properties();
            properties.load(resourceAsStream);

            String driver = properties.getProperty("driver");
            String url = properties.getProperty("url");
            String user = properties.getProperty("username");
            String password = properties.getProperty("password");

            Class.forName(driver);

            connection = DriverManager.getConnection(url, user, password);

            statement = connection.createStatement();

            String sql = "select id,username as name, password, salary from tb_user";
            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                // 1、根据索引，以字符串接收数据
                // String id = resultSet.getString(1);
                // String name = resultSet.getString(2);
                // String pwd = resultSet.getString(3);
                // String salary = resultSet.getString(4);

                // 2、根据索引，以指定类型接收数据
                // int id = resultSet.getInt(1);
                // String name = resultSet.getString(2);
                // String pwd = resultSet.getString(3);
                // double salary = resultSet.getDouble(4);

                // 3、根据字段名，以字符串接收数据
                // String id = resultSet.getString("id");
                // String name = resultSet.getString("username");
                // String pwd = resultSet.getString("password");
                // double salary = resultSet.getDouble("salary");

                // 4、根据字段名，以指定类型接收数据
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");   // 重命名之后的名称
                String pwd = resultSet.getString("password");
                double salary = resultSet.getDouble("salary");

                System.out.println(id + "\t" + name + "\t" + pwd + "\t" + salary);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
